package com.derbysoft.lowcostadapter.web.action.admin.transactionlog;

import com.derbysoft.common.web.struts2.support.PaginateActionSupport;
import com.derbysoft.lowcostadapter.commons.model.*;
import com.derbysoft.lowcostadapter.commons.repository.ReservationDetailRepository;
import com.derbysoft.lowcostadapter.commons.repository.ReservationLogRepository;
import com.derbysoft.lowcostadapter.commons.repository.TransactionDetailRepository;
import com.derbysoft.lowcostadapter.commons.repository.TransactionLogRepository;
import com.derbysoft.lowcostadapter.dswitch3.service.AvailRequestMonitor;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class RequestLogAction extends PaginateActionSupport {

    @Autowired
    private TransactionLogRepository transactionLogRepository;

    @Autowired
    private ReservationLogRepository reservationLogRepository;

    @Autowired
    private TransactionDetailRepository transactionDetailRepository;

    @Autowired
    private ReservationDetailRepository reservationDetailRepository;

    @Autowired
    private AvailRequestMonitor availRequestMonitor;

    private AvailRequestCounter availRequestCounter = new AvailRequestCounter();

    private RequestLogCondition requestCondition;

    private TransactionLog transactionLog;

    private ReservationLog reservationLog;

    private List<TransactionDetail> transactionDetails;

    private List<ReservationDetail> reservationDetails;

    @Action("list")
    public String list() {
        ActionContext.getContext().setLocale(ServletActionContext.getRequest().getLocale());
        if (requestCondition == null) {
            return SUCCESS;
        }
        if (requestCondition.getCode().equals(OperationType.HOTEL_AVAIL)) {
            transactionLogRepository.findByCondition(requestCondition, getPaginater());
        } else {
            reservationLogRepository.findByCondition(requestCondition, getPaginater());
        }
        return SUCCESS;
    }

    @Actions({@Action("view-ajax"), @Action("view")})
    public String view() {
        if (requestCondition.getCode() == null) {
            return INPUT;
        }
        if (requestCondition.getCode().equals(OperationType.HOTEL_AVAIL)) {
            transactionLog = transactionLogRepository.findById(Long.valueOf(requestCondition.getId()));
            if (transactionLog != null) {
                transactionDetails = transactionDetailRepository.find(transactionLog);
                transactionLog.setTransactionDetails(transactionDetails);
            }
        } else {
            reservationLog = reservationLogRepository.findById(Long.valueOf(requestCondition.getId()));
            if (reservationLog != null) {
                reservationDetails = reservationDetailRepository.find(reservationLog);
                reservationLog.setReservationDetails(reservationDetails);
            }
        }
        return SUCCESS;
    }

    public RequestLogCondition getRequestCondition() {
        return requestCondition;
    }

    public void setRequestCondition(RequestLogCondition requestCondition) {
        this.requestCondition = requestCondition;
    }

    public List<TransactionDetail> getTransactionDetails() {
        return transactionDetails;
    }

    public ReservationLog getReservationLog() {
        return reservationLog;
    }

    public TransactionLog getTransactionLog() {
        return transactionLog;
    }

    public List<ReservationDetail> getReservationDetails() {
        return reservationDetails;
    }

    public void setReservationDetails(List<ReservationDetail> reservationDetails) {
        this.reservationDetails = reservationDetails;
    }

    public AvailRequestCounter getAvailRequestCounter() {
        availRequestCounter.setTotalCount(availRequestMonitor.getTotalCount().get());
        availRequestCounter.setRealCheckCount(availRequestMonitor.getRealCheckCount().get());
        availRequestCounter.setNoAllotmentCount(availRequestMonitor.getNoAllotmentCount().get());
        availRequestCounter.setQueryCacheCount(availRequestMonitor.getQueryCacheCount().get());
        availRequestCounter.setMaxOccRestrictedCount(availRequestMonitor.getOccupancyRestrictedCount().get());
        availRequestCounter.setRateHitCount(availRequestMonitor.getRateHitCount().get());
        availRequestCounter.setRoomStayFilteredCount(availRequestMonitor.getRoomStayFilteredCount().get());
        return availRequestCounter;
    }
}